/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Snake2.java
 *
 * Created on 19-mar-2010, 9.42.20
 */
package frame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

/**
 *
 * @author paolo
 */
public class Snake2 extends javax.swing.JFrame {

    Move m;

    private class Tasti extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    m.setDirezione("left");
                    jLabel1.setIcon(new ImageIcon(getClass().getResource("/frame/snake.png")));
                    jLabel1.setSize(40,20);
                    break;
                case KeyEvent.VK_RIGHT:
                    m.setDirezione("right");
                    jLabel1.setIcon(new ImageIcon(getClass().getResource("/frame/snake.png")));
                    jLabel1.setSize(40,20);
                    break;
                case KeyEvent.VK_UP:
                    m.setDirezione("up");
                    jLabel1.setIcon(new ImageIcon(getClass().getResource("/frame/snake2.png")));
                    jLabel1.setSize(20,40);
                    break;
                case KeyEvent.VK_DOWN:
                    m.setDirezione("down");
                    jLabel1.setIcon(new ImageIcon(getClass().getResource("/frame/snake2.png")));
                    jLabel1.setSize(20,40);
                    break;
            }
        }
    }

    private class Move extends Thread {

        private String direzione;

        public Move() {
            this.direzione = "left";
        }

        public void setDirezione(String pDirezione) {
            this.direzione = pDirezione;
        }

        @Override
        public void run() {
            while (true) {
                int x = jLabel1.getLocation().x;
                int y = jLabel1.getLocation().y;
                if (this.direzione.equals("left")) {
                    if (x - 10 > 0) {
                        jLabel1.setLocation(x - 10, y);
                    }
                }
                if (this.direzione.equals("right")) {
                    if (x + 10 < 390) {
                        jLabel1.setLocation(x + 10, y);
                    }
                }
                if (this.direzione.equals("up")) {
                    if (y - 10 > 0) {
                        jLabel1.setLocation(x, y - 10);
                    }
                }
                if (this.direzione.equals("down")) {
                    if (y + 10 < 290) {
                        jLabel1.setLocation(x, y + 10);
                    }
                }
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
    }

    /** Creates new form Snake2 */
    public Snake2() {
        initComponents();
        this.setFocusable(true);
        addKeyListener(new Tasti());
        this.m = new Move();
        this.m.setDirezione("right");
        this.m.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Snake!");

        jPanel1.setBackground(new java.awt.Color(118, 204, 102));
        jPanel1.setLayout(null);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frame/snake.png"))); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 40, 20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Snake2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
